package com.aso114.project.mvp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseFragment;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.mvp.activity.fragment.PracticeHistoryOneFragment;
import com.aso114.project.mvp.activity.fragment.PracticeHistoryTwoFragment;
import com.aso114.project.util.Constant;
import com.aso114.project.util.SystemBarHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 练习历史
 * Created by asus on 2018/11/8.
 */

public class PracticeHistoryActivity extends BaseSimpleActivity {
    @BindView(R.id.practive_back)
    ImageView practiveBack;
    @BindView(R.id.practice_left)
    TextView practiceLeft;
    @BindView(R.id.practive_right)
    TextView practiveRight;
    @BindView(R.id.practive_fragment)
    FrameLayout practiveFragment;
    public static String catid = Constant.adutst;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;
    private boolean isLeft = true;

    PracticeHistoryOneFragment practiceHistoryOneFragment;
    PracticeHistoryTwoFragment practiceHistoryTwoFragment;

    @Override
    protected int getResouseId() {
        return R.layout.activity_practice_history;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(this, 0);
            SystemBarHelper.setStatusBarDarkMode(this);
        }
        catid = getIntent().getStringExtra("catid");
        if ("367".equals(catid)) {
            isLeft = false;
        }
        setView();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.practive_back, R.id.practice_left, R.id.practive_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.practive_back:
                finish();
                break;
            case R.id.practice_left:
                if (isLeft)
                    return;
                isLeft = true;
                setView();
                break;
            case R.id.practive_right:
                if (!isLeft)
                    return;
                isLeft = false;
                setView();
                break;
        }
    }

    private void setView() {
        practiceLeft.setSelected(false);
        practiveRight.setSelected(false);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();


        if (isLeft) {
            if (practiceHistoryOneFragment == null) {
                practiceHistoryOneFragment = new PracticeHistoryOneFragment();
                transaction.add(R.id.practive_fragment, practiceHistoryOneFragment);
            }
            practiceLeft.setSelected(true);
            initFragment(transaction, practiceHistoryOneFragment);
        } else {
            if (practiceHistoryTwoFragment == null) {
                practiceHistoryTwoFragment = new PracticeHistoryTwoFragment();
                transaction.add(R.id.practive_fragment, practiceHistoryTwoFragment);
            }
            practiveRight.setSelected(true);
            initFragment(transaction, practiceHistoryTwoFragment);
        }


    }

    //显示fragment
    private void initFragment(FragmentTransaction transaction, BaseFragment currentFragment) {
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(currentFragment);
        //提交事务
        transaction.commit();
    }

    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (practiceHistoryOneFragment != null) {
            transaction.hide(practiceHistoryOneFragment);
        }
        if (practiceHistoryTwoFragment != null) {
            transaction.hide(practiceHistoryTwoFragment);
        }
    }
}
